package org.xfy.sys.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.BusinessKey;
import org.xfy.base.util.EmailUtil;
import org.xfy.org.domain.Logininfo;
import org.xfy.org.service.ILogininfoService;
import org.xfy.sys.domain.User;
import org.xfy.sys.domain.dto.EmailUserRegisterDto;
import org.xfy.sys.domain.dto.PhoneUserRegisterDto;
import org.xfy.sys.mapper.UserMapper;
import org.xfy.sys.service.IRegisterService;
@Service
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ILogininfoService logininfoService;
    @Autowired
    private EmailUtil emailUtil;

    /**
     * 手机注册
     * @param registerDto
     * @return
     */
    @Override
    public AjaxResult UserRegister(PhoneUserRegisterDto registerDto) {
        //1.验证手机验证
        String key= BusinessKey.BK_REGISTER+BusinessKey.BK_SPLIC+registerDto.getPhone();
        if (!redisTemplate.hasKey(key)) {
             AjaxResult.me().setSuccess(false).setMessage("手机验证码已过期");
        }
        Object o = redisTemplate.opsForValue().get(key);
        if(!o.toString().equals(registerDto.getPhoneCode())){
            AjaxResult.me().setSuccess(false).setMessage("手机验证码不匹配");
        }
        //2.比较密码是否正确
        if(registerDto.getPassword().equals(registerDto.getPasswordRepeat())){
            AjaxResult.me().setSuccess(false).setMessage("两次密码输入不一致");
        }
        //3.验证手机是否被注册
        Long count=userMapper.findCountByPhone(registerDto.getPhone());
        if(count!=0){
            AjaxResult.me().setSuccess(false).setMessage("手机已被注册");
        }
        //4.loginfo,User加入数据库
        logininfoService.register(registerDto,1);
        return AjaxResult.me();
    }

    /**
     * 邮箱注册
     * @param registerDto
     * @return
     */
    @Override
    public AjaxResult EmailRegister(EmailUserRegisterDto registerDto) {
        if(!registerDto.getPassword().equals(registerDto.getPasswordRepeat())){
            AjaxResult.me().setSuccess(false).setMessage("两次密码输入不一致");
        }
        String email = registerDto.getEmail();
        User oneByEmail = userMapper.findOneByEmail(email);
        if(oneByEmail!=null){
            AjaxResult.me().setSuccess(false).setMessage("邮箱已被注册");
        }
        //发送邮件激活
        String url = "<a href='http://127.0.0.1:8080/register/activation/" + email + "'>点我激活</a>";
        emailUtil.sendEmail("账号激活",url,email);
        logininfoService.emailregister(registerDto,1);
        return AjaxResult.me();
    }

    /**
     * 邮箱激活
     * @param email
     * @return
     */
    @Override
    @Transactional
    public AjaxResult activation(String email) {
        User user = userMapper.findOneByEmail(email);
        user.setState(1);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        logininfoService.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);
        return AjaxResult.me();
    }
}
